package com.meixiang.activity.account.manager;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.util.AbToastUtil;

import org.json.JSONObject;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by wzz
 * 意见反馈
 */
public class FeedBackActivity extends BaseActivity {


    @Bind(R.id.ce_feedback)
    EditText mCeFeedback;
    @Bind(R.id.tv_feed)
    TextView mTvFeed;
    @Bind(R.id.tv_login)
    TextView mTvLogin;
    private boolean textFlag=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_feed_back);

        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);


    }

    @Override
    protected void initTitle() {
        setTitle(getResources().getString(R.string.feedback));
    }

    @Override
    protected void initView() {
      //  mCeFeedback.addTextChangedListener(watcher);

        //设置EditText的显示方式为多行文本输入
        mCeFeedback.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        //文本显示的位置在EditText的最上方
        mCeFeedback.setGravity(Gravity.TOP);
        //改变默认的单行模式
        mCeFeedback.setSingleLine(false);
        //水平滚动设置为False
        mCeFeedback.setHorizontallyScrolling(false);
    }

    @Override
    protected void setData() {
        initEvent();
    }


//    private TextWatcher watcher = new TextWatcher() {
//
//        @Override
//        public void onTextChanged(CharSequence s, int start, int before, int count) {
//            // TODO Auto-generated method stub
//            if (mCeFeedback.getText().toString().length() > 0&&mCeFeedback.getText().toString().length()<199) {
//                int feed = mCeFeedback.getText().toString().length();
//                int tvFeed = 200 - feed;
//                mTvFeed.setText(tvFeed + "");
//            }else
//                mTvFeed.setText("200");
//
//        }
//
//        @Override
//        public void beforeTextChanged(CharSequence s, int start, int count,
//                                      int after) {
//            // TODO Auto-generated method stub
//
//        }
//
//        @Override
//        public void afterTextChanged(Editable s) {
//            // TODO Auto-generated method stub
//
//        }
//    };

    @Override
    @OnClick(R.id.tv_login)
    public void onClick(View v) {
        if (TextUtils.isEmpty(mCeFeedback.getText().toString().trim())) {
            AbToastUtil.showToast(context, "请输入您的宝贵建议");
            return;
        } else
            getData();
    }

    /**
     * /feedbackApi/feeback
     * 输入参数
     * <p/>
     * 名称	类型	是否必须	示例值	默认值	描述
     * phone	String	选填	13666666666		电话
     * email	String	选填			邮箱地址
     * phoneType	Integer	必填	1	3	设备类型 ,1 ios ,2 安卓，3 其他
     * content	String	必填	点订单列表的时闪退啦		意见的正文 99字
     * title	String	必填	闪退啦		意见的标题 49个字
     */
    private void getData() {
        HttpParams params = new HttpParams();
        params.put("content", mCeFeedback.getText().toString());
        params.put("deviceType", "2");//设备类型 ,1 ios ,2 安卓，3 其他
        HttpUtils.post(Config.SERVER_URL + "saveFeedBack", params, new HttpCallBack(activity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if ("0".equals(code)) {
                    finish();
                }
                AbToastUtil.showToast(context, message);
            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(context, message);
            }
        });
    }
    private void initEvent()
    {
        mCeFeedback.addTextChangedListener(new TextWatcher()
        {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
                           {
                    if(textFlag) {
                        if (mCeFeedback.getText().toString().length() > 0 && mCeFeedback.getText().toString().length() < 199) {
                            int feed = mCeFeedback.getText().toString().length();

                            textFlag = false;
                            String text = mCeFeedback.getText().toString();
                            mCeFeedback.setText(getTextValue(text));
                        }
                    }
//                    }else
//                        mTvFeed.setText("200");
//                    }


            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {

            }

            @Override
            public void afterTextChanged(Editable s)
            {
                mTvFeed.setText(200-mCeFeedback.getText().length()+"");
                textFlag=true;
                if(s instanceof Spannable)
                {
                    Spannable spanText = (Spannable)s;
                    Selection.setSelection(spanText, s.length());

                }
            }
        });
    }

    private String getTextValue(String str)
    {
        String s=new String();
        for(int i=0;i<str.length();i++)
        {
            String text=str.substring(i, i+1);
            Log.i("text_info", text);
           // [a-zA-Z0-9\.\/]
            Pattern p = Pattern.compile("[0-9]*");
            Matcher m = p.matcher(text);
            if (m.matches())
            {
                s+=text;
            }
            p = Pattern.compile("[a-zA-Z]");
            m = p.matcher(text);
            if (m.matches())
            {
                s+=text;
            }
            p = Pattern.compile("[\u4e00-\u9fa5]");
            m = p.matcher(text);
            if (m.matches())
            {
                s+=text;
            }
            p = Pattern.compile("[\\,\\/\\.\\/\\。\\/\\，\\/\\；\\/\\;\\/\\！\\/\\!\\/\\？\\/\\?\\/\\：\\/\\:\\/\\()\\/\\（）\\/\\{}\\/\\{}\\/\\《》… …… ＜＞\\/]");
            m = p.matcher(text);
            if (m.matches())
            {
                s+=text;
            }

        }

      //  mCeFeedback.setText(s.trim());
        return s.trim();
    }

}
